<?php

namespace app\admin\controller;


use think\Request;

class Picture extends Base
{
    //滚图列表
    public function all()
    {
        //查询
        $pictures = model('Picture')->order('create_time','desc')->paginate(3);
        //模板变量
        $viewData = [
            'pictures' => $pictures
        ];
        //渲染
        $this->assign($viewData);
        return view();
    }

    //滚图添加显示
    public function index()
    {
        return view();
    }

    //滚图添加
    public function add(Request $request)
    {
        $data = $request->param();
        //创建验证器
        $validate = new \app\common\validate\Picture();
        if (!$validate->check($data)){
            return ['code' => 0,'msg' => $validate->getError()];
        }
        //创建模型
        $db = new \app\common\model\Picture();
        //接收data
        $result = $db->_update($data);
        if ($result){
            return ['code' => 1,'msg' => '上传成功'];
        }else{
            return ['code' => 0,'msg' => '上传失败'];
        }
    }

    //编辑
    public function edit(Request $request)
    {
        //接收id
        $id = $request->param('id');
        //实例化模型
        $db = new \app\common\model\Picture();
        //查询图片
        $pictureInfo = $db->where('id',$id)->find();
        //模板变量
        $viewData = [
            'pictureInfo' => $pictureInfo
        ];
        //渲染
        $this->assign($viewData);
        return view();
    }

    //删除
    public function del()
    {
        //查询图片id
        $pictureInfo = model('Picture')->find(input('post.id'));
        //删除
        $result = $pictureInfo->delete();
        if ($result){
            $this->success('删除成功','admin/picture/all');
        }else{
            $this->error('删除失败');
        }
    }
}
